package login;

import dbTools.DBTools;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Servlet implementation class Loginserver
 */
@WebServlet("/Loginserver")
public class Loginserver extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Loginserver() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;chatset=UTF-8");
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK");
		String userName=request.getParameter("userName");
		String userPwd=request.getParameter("userPwd");
	 	Boolean verify = false;
		DBTools db = new DBTools();
		db.open();
		
		String sql="select * from userInfo where userName='"+userName+"' and userPwd='"+userPwd+"'";
		ResultSet rs = db.select(sql);
		try{
			while(rs.next()){
				if(rs.getString("userName").equals(userName)){
					verify=true;
				}
			}
		}catch(SQLException e){
			e.printStackTrace();
		}if(verify==true){
			HttpSession session=request.getSession();
			session.setAttribute("userName", userName);
			session.setAttribute("error", false);
			request.getRequestDispatcher("/index.jsp").forward(request, response);
		}else{
			request.getRequestDispatcher("/login.jsp").forward(request, response);
		}
	}

}
